<template>
  <div class="app-container">
    <el-card>
      <!--搜索表单开始-->
      <el-form :inline="true" size="small" :model="page.params">
        <el-form-item>
          <el-input v-model="page.params.roleName" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select clearable v-model="page.params.status" placeholder="请选择状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-permission="['system:role:query']" size="small" @click="search">查询</el-button>
          <el-button type="warning" icon="el-icon-refresh" size="small" @click="page.params={}">重置</el-button>
        </el-form-item>
      </el-form>
      <!--搜索表单结束-->
      <!--角色数据表格开始-->
      <div>
        <div style="margin-bottom: 5px">
          <el-button  type="success" v-permission="['system:role:add']"  icon="el-icon-plus" size="small"
                     @click="toAdd">添加
          </el-button>
        </div>
        <el-table
          v-loading="loading"
          :data="dataPage.list"
          border
          stripe
          style="width: 100%"
        >
          <el-table-column prop="roleId" label="角色编号" align="center"></el-table-column>
          <el-table-column prop="roleName" label="角色名称" align="center"></el-table-column>
          <el-table-column prop="roleSort" label="排序码" align="center"></el-table-column>
          <el-table-column prop="remark" label="角色备注" align="center"></el-table-column>
          <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="{row}">
              <dict-tag :data-list="statusOptions" :value="row.status"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="{row}">
              <el-dropdown>
                    <span class="el-dropdown-link">
                      <el-link icon="el-icon-s-operation" type="primary">操作<i
                        class="el-icon-arrow-down el-icon--right"></i></el-link>
                    </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-permission="['system:role:update']">
                    <el-button type="text" icon="el-icon-edit"  @click="toUpdate(row)">修改</el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['system:role:remove']">
                    <el-button type="text" icon="el-icon-delete" @click="toDelete(row)">删除</el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['system:role:selectPermission']">
                    <el-button type="text" icon="el-icon-setting" @click="toSelectMenu(row)">分配权限</el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNumber"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataPage.totalCount">
        </el-pagination>
      </div>
      <!--角色数据表格结束-->
    </el-card>

    <!--角色添加的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="addDialog"
               v-if="addDialog">
      <sys-role-add @after="getByPage" @close="closeDialog"></sys-role-add>
    </el-dialog>
    <!--角色添加的弹出层结束-->

    <!--角色修改的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="updateDialog"
               v-if="updateDialog">
      <sys-role-update :active-id="activeId" @after="getByPage"
                            @close="closeDialog"></sys-role-update>
    </el-dialog>
    <!--角色修改的弹出层结束-->

    <!--分配权限的弹出层开始-->
    <el-dialog :title="title"
               center
               width="30%"
               :visible.sync="selectMenuDialog"
               v-if="selectMenuDialog">
      <sys-role-select-menu :active-id="activeId"  @close="closeDialog"></sys-role-select-menu>
    </el-dialog>
    <!--分配权限的弹出层结束-->

  </div>
</template>

<script>
import sysRoleApi from "@/api/system/sysRole";
import sysRoleAdd from "./sys-role-add"
import sysRoleUpdate from "./sys-role-update"
import sysRoleSelectMenu from "./sys-role-select-menu"
export default {
  name: "sys-role-list",
  components: {
    sysRoleAdd,
    sysRoleUpdate,
    sysRoleSelectMenu
  },
  data() {
    return {
      //是否显示加载中的进度条
      loading: false,
      //查询条件
      page: {
        pageNumber: 1,
        pageSize: 10,
        params: {
          roleName: undefined,
          status: undefined
        }
      },
      //角色的表格数据
      dataPage: {
        list: [],
        totalCount: 0
      },
      //角色状态的字典数据
      statusOptions: [],
      //弹出层的标题
      title:"",
      //是否打开添加角色的弹出层
      addDialog:false,
      //是否打开修改角色的弹出层
      updateDialog:false,
      //当前点击行的roleId
      activeId:undefined,
      //是否打开分配权限的弹出层
      selectMenuDialog:false
    }
  },
  created() {
    //加载角色数据
    this.getByPage()
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
  },
  methods: {
    //加载角色数据
    getByPage() {
      this.loading = true
      sysRoleApi.getByPage(this.page).then(res => {
        this.dataPage.list = res.data.list
        this.dataPage.totalCount = res.data.totalCount
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    //每页条数发生变化时触发
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getByPage()
    },
    //页码发生变化时触发
    handleCurrentChange(val) {
      this.page.pageNumber = val;
      this.getByPage()
    },
    //搜索
    search() {
      this.getByPage();
      this.dictDataList = []
    },
    //打开添加的弹出层
    toAdd() {
      this.title="添加角色"
      this.addDialog=true;
    },
    //打开修改的弹出层
    toUpdate(row) {
      this.updateDialog=true;
      this.title=`修改【${row.roleName}】角色`
      this.activeId=row.roleId
    },
    //删除
    toDelete(row) {
      this.$confirm(`此操作会删除【${row.roleName}】该角色数据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysRoleApi.deleteById(row.roleId).then(res => {
          this.$message.success('删除成功!');
          this.getByPage();
        })
      }).catch(() => {
        this.$message.info('已取消删除!');
      });
    },
    //打开分配权限的弹出层
    toSelectMenu(row) {
      this.selectMenuDialog=true;
      this.title=`给【${row.roleName}】角色分配权限菜单`
      this.activeId=row.roleId
    },
    //关闭所有弹出层
    closeDialog() {
      this.addDialog = false;
      this.updateDialog = false;
    }
  }
}
</script>

<style scoped>

</style>
